package com.twenty.seven.security.sign.aop;

import com.twenty.seven.security.sign.entity.TripartiteCode;
import com.twenty.seven.security.sign.entity.TripartiteEntity;
import com.twenty.seven.security.sign.service.SignService;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @Author 001
 * @Date 2020/1/16 15:02
 * @TODO 第三方接口签名校验
 **/
@Aspect
@Order(1)
@Component
public class TripartitePlatformSecurity {

    private Logger logger = LoggerFactory.getLogger(TripartitePlatformSecurity.class);

    @Autowired
    private SignService signService;

    @Pointcut("execution(public * com.twenty.seven.security.sign.controller.TripartitePlatformController.*(..))&& args(tripartiteEntity,..)")
    public void pointTripartitePlatform(TripartiteEntity tripartiteEntity){}

    @Before("pointTripartitePlatform(tripartiteEntity)")
    public void runBefore(JoinPoint joinPoint,TripartiteEntity tripartiteEntity){

        preMsy(joinPoint);
        if(StringUtils.isBlank(tripartiteEntity.getAccessKey()) || StringUtils.isBlank(tripartiteEntity.getSign())){
            logger.error(">>>参数异常");
            tripartiteEntity.setVerifyCode(TripartiteCode.paramError.getCode());
        }else{
            try {
                boolean flag = signService.verify(tripartiteEntity.getSign(),tripartiteEntity.getAccessKey());
                if(flag){
                    logger.info(">>>>>>>>>>通过校验>>>>>");
                    tripartiteEntity.setVerifyCode(TripartiteCode.success.getCode());
                }else{
                    logger.info(">>>>>>校验失败>>>>>>");
                    tripartiteEntity.setVerifyCode(TripartiteCode.fault.getCode());
                }
            } catch (Exception e) {
                e.printStackTrace();
                logger.error(">>>>>>>校验失败");
                tripartiteEntity.setVerifyCode(TripartiteCode.fault.getCode());
            }
        }

    }

    @AfterReturning(pointcut = "pointTripartitePlatform(tripartiteEntity)")
    public void runAfterReturning(TripartiteEntity tripartiteEntity){
        logger.info(">>>>>>>结束>>>>>>");
    }

    private void preMsy(JoinPoint joinPoint){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        logger.info("<<<<<<<<<<<<<<<<<<<<<<第三方接口请求校验开始");
        logger.info(">>>>>>>>>>>>请求来源信息");
        logger.info(">>>>>>>URL :{}",request.getRequestURL()+"["+request.getMethod()+"]");
        logger.info(">>>>>>IP:{}",request.getRemoteHost());

        Enumeration<String> headerNames = request.getHeaderNames();
        logger.info(">>>CLASS_METHOD : {}",joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
    }
}
